package com.bskj.kuaifu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.common.bean.model.ParkInfo;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.bean.model.UserMoneyLog;
import com.bskj.common.constants.CommonConstatns;
import com.bskj.common.dao.UserMoneyLogDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.UserMoneyChangeTypeEnum;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.kuaifu.bean.vo.resp.UserMoneyLogRespVo;
import com.bskj.kuaifu.service.IParkInfoService;
import com.bskj.kuaifu.service.IParkingLotCustomerService;
import com.bskj.kuaifu.service.IUserMoneyLogService;

/**
 * @author lishi
 * @date 2017-11-29 下午4:03:25
 */
@Service
public class UserMoneyLogServiceImpl extends BaseServiceImpl<UserMoneyLog> implements IUserMoneyLogService {
    @Autowired
    private UserMoneyLogDao userMoneyLogDao;

    @Autowired
    private IParkingLotCustomerService parkingLotCustomerService;

    @Autowired
    private IParkInfoService parkInfoService;

    public BaseDao<UserMoneyLog> getBaseDao() {
        return userMoneyLogDao;
    }

    public List<UserMoneyLogRespVo> getUserMoneyLog(Integer userId, UserMoneyChangeTypeEnum userMoneyChangeTypeEnum,
                                                    Integer page, Integer pageSize) throws Exception {
        if (page == null || page == 0) {
            page = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CommonConstatns.DEFAULT_PAGE_SZIE;
        }
        FindRequestEntity findRequestEntity = new FindRequestEntity();
        findRequestEntity.addWhere("user_id", WhereEnum.EQ, userId);
        if (UserMoneyChangeTypeEnum.DEL.equals(userMoneyChangeTypeEnum)) {
            findRequestEntity.addWhere("change_money", WhereEnum.GT, 0);
        }
        findRequestEntity.addWhere("change_type", WhereEnum.EQ, userMoneyChangeTypeEnum.getKey());

        findRequestEntity.addOrder("create_time", OrderEnum.DESC);

        findRequestEntity.setPage(page, pageSize);

        List<UserMoneyLog> userMoneyLogList = find(findRequestEntity);
        List<UserMoneyLogRespVo> resultList = new ArrayList<UserMoneyLogRespVo>();
        if (userMoneyLogList != null && userMoneyLogList.size() > 0) {
            for (UserMoneyLog userMoneyLog : userMoneyLogList) {
                UserMoneyLogRespVo respVo = new UserMoneyLogRespVo();
                BeanUtils.copyProperties(userMoneyLog, respVo);
                //微信提现：
                if (userMoneyLog.getRefundInfoId() != null) {
                    respVo.setParkLotArea("微信账户提现");
                    respVo.setParkLotName("账户提现");
                } else if (UserMoneyChangeTypeEnum.DEL.equals(userMoneyChangeTypeEnum)) {
                    fullParkInfo(respVo);
                }
                resultList.add(respVo);
            }
        }
        return resultList;
    }

    private void fullParkInfo(UserMoneyLogRespVo userParkInfoRespVo) throws Exception {
        ParkInfo parkInfo = parkInfoService.get(userParkInfoRespVo.getParkInfoId());
        if (parkInfo != null) {
            userParkInfoRespVo.setCarNo(parkInfo.getCarNo());
            ParkingLotCustomer parkingLotCustomer = parkingLotCustomerService.get(parkInfo.getParkLotCustimerId());
            if (parkingLotCustomer != null) {
                userParkInfoRespVo.setParkLotArea(parkingLotCustomer.getAreaName());
                userParkInfoRespVo.setParkLotName(parkingLotCustomer.getName());
            }
        }
    }
}
